package com.example.myapplication;

import static com.example.myapplication.DataBaseHelper.getInstance;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.provider.ContactsContract;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

import java.io.IOException;

public class MainActivity extends Activity {
     private ImageButton startButton,levelsButton,helpButton;//imageButton
    private DataBaseHelper databaseHelper;//数据库有关内容
    private long waitTime = 2000, touchTime = 0;//等待和触碰时间


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        databaseHelper = getInstance(this);

        startButton = findViewById(R.id.main_start_button);
        levelsButton =  findViewById(R.id.main_levels_button);
        helpButton =  findViewById(R.id.main_help_button);
        //各个组件会通过andriod:id 属性给每个组件设置一个唯一的标识。当需要在代码中访问指定的组件时（例如设置单击事件），就可以通过id值，利用方法findViewById(R.id.id值)来访问。
        startButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                WarRecord war = databaseHelper.getWar((int)(Math.random() * 394));
                Intent intent = new Intent(MainActivity.this, ChessBoardActivity.class);
                intent.putExtra("WARID", war.getId());//intent.putExtra("msg", msg);将数据传至另一个界面
                startActivity(intent);
            }
        });
        //不需要返回数据的方式
//        Intent intent = new Intent(MainActivity.this,SecondActivity.class);
//
//        startActivity(intent);
        levelsButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, LevelsActivity.class);
                startActivity(intent);
            }
        });
        helpButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,HelpActivity.class );
                startActivity(intent);
            }
        });

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    @Override
    public void onBackPressed() {
        long currentTime = System.currentTimeMillis();
        if((currentTime-touchTime)>=waitTime) {
            Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT).show();
            touchTime = currentTime;
        }else {
            finish();
        }
    }


}